/**
 * 当前webpack.config.js文件已废弃，仅当参考！
 * 请移步至config文件夹
 */
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { DefinePlugin } = require('webpack');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  devtool: 'source-map', // 控制生成source-map源文件映射
  output: {
    filename: 'js/main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  target: 'web',
  resolve: {
    // 导入语句是否需要带文件后缀
    enforceExtension: false,
    // 后缀补全
    extensions: ['.js', '.json', '.jsx', '.vue', '.ts', '.css'],
    // 文件别名
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@api': path.resolve(__dirname, 'src/api'),
      '@utils': path.resolve(__dirname, 'src/utils'),
      '@assets': path.resolve(__dirname, 'src/assets'),
    }
  },
  devServer: {
    hot: true,
    compress: true, // 压缩
    proxy: {
      "/api": {
        target: 'http://api.github.com',
        pathRewrite: { "^/api": "" },
        changeOrigin: true,
      }
    },
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            }
          },
          'postcss-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2
            }
          },
          'postcss-loader',
          'less-loader'
        ]
      },
      {
        test: /\.(png|svg|gif|jpe?g)$/,
        type: 'asset',
        generator: {
          filename: 'img/[name].[hash:4][ext]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 50 * 1024
          }
        }
      },
      {
        test: /\.(ttf|woff2?)$/,
        type: 'asset/resource',
        generator: {
          filename: 'font/[name].[hash:3][ext]'
        }
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.vue$/,
        use: ['vue-loader']
      },
      {
        test: /\.ts$/,
        use: ['babel-loader']
      }
    ]
  },
  plugins: [
    // 自动清除打包文件夹dist
    new CleanWebpackPlugin(),
    // 生成index.html模板
    new HtmlWebpackPlugin({
      title: 'webpack5实战',
      template: "./public/index.html",
    }),
    // 复制文件
    new CopyWebpackPlugin({
      patterns: [
        {
          from: 'public',
          globOptions: {
            ignore: ['**/index.html']
          }
        }
      ]
    }),
    // webpack自带定义属性方法(重点，看写法)
    new DefinePlugin({
      BASE_URL: '"./"'
    }),
    // vue-loader@15需要此配置
    new VueLoaderPlugin(),
    // react组件热更新插件
    new ReactRefreshWebpackPlugin()
  ]
}